package com.fare.taluo.application.impl.plane.mycare;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.freight.FreightMonitorSettingsApplication;
import com.fare.taluo.core.domain.analyse.monitor.FreightMonitorSettingsEntity;
import com.fare.taluo.vo.freight.FreightMonitorSettingsVO;

/*
 * @xiaozhi
 * Email:ganhongzhi@foreveross.com
 * this is 运价监控设置 ApplicationImpl
*/

@Transactional
@Scope("singleton")
@Service("freightMonitorSettingsApplication")
public class FreightMonitorSettingsApplicationImpl implements FreightMonitorSettingsApplication{	
	
	public List<FreightMonitorSettingsVO> findFreightMonitorVO(String currentUser) throws Exception{
		FreightMonitorSettingsEntity freightMonitorEntity = new FreightMonitorSettingsEntity();
		List <FreightMonitorSettingsVO>  volist=new ArrayList<FreightMonitorSettingsVO>();
		List<FreightMonitorSettingsEntity> entitylist=freightMonitorEntity.findFreightMonitorAll(currentUser);
		if(entitylist !=null && entitylist.size()>0){
			for(FreightMonitorSettingsEntity entity:entitylist){
				FreightMonitorSettingsVO vo=new FreightMonitorSettingsVO();
				vo.setId(entity.getId());
				vo.setAirLine(entity.getFromCity()+"-"+entity.getToCity());
				vo.setDepartDate(entity.getDepartDate());
				vo.setFlightNumber(entity.getFlightNumber());
				vo.setOpDate(entity.getOpDate());
				vo.setCompanyCode(entity.getCompanyCode());
				vo.setCurrentUser(entity.getCurrentUser());
				vo.setFromCity(entity.getFromCity());
				vo.setFromCode(entity.getFromCode());
				vo.setToCity(entity.getToCity());
				vo.setToCode(entity.getToCode());
				volist.add(vo);
			}
		}
		
		return volist;
	}
	public void delete(Long id) {
		FreightMonitorSettingsEntity entity = new FreightMonitorSettingsEntity();		
		entity.setId(id);
		entity.delete(entity);
	}
	
	public List<FreightMonitorSettingsVO> findLineByConditions(Date queryDate,Date toQueryDate,String fromCode,String toCode,Date createTime,Date toCreateTime,String currentUsers){
		FreightMonitorSettingsEntity freightMonitorEntity = new FreightMonitorSettingsEntity();
		List<FreightMonitorSettingsEntity> list=freightMonitorEntity.findOccupRateEntityByCondition(queryDate, toQueryDate, fromCode, toCode, createTime, toCreateTime,currentUsers);
		
		return FreightMonitorSettingsEntityVO(list);
	}
	
	public List<FreightMonitorSettingsVO> FreightMonitorSettingsEntityVO(List<FreightMonitorSettingsEntity> list){
		List<FreightMonitorSettingsVO> voList =new ArrayList<FreightMonitorSettingsVO>();
		for(FreightMonitorSettingsEntity entity : list){
			FreightMonitorSettingsVO vo = new FreightMonitorSettingsVO();
			vo.setId(entity.getId());
			vo.setAirLine(entity.getFromCity()+"-"+entity.getToCity());
			vo.setDepartDate(entity.getDepartDate());
			vo.setFlightNumber(entity.getFlightNumber());
			vo.setOpDate(entity.getOpDate());
			vo.setCompanyCode(entity.getCompanyCode());
			vo.setCurrentUser(entity.getCurrentUser());
			vo.setFromCity(entity.getFromCity());
			vo.setFromCode(entity.getFromCode());
			vo.setToCity(entity.getToCity());
			vo.setToCode(entity.getToCode());
			voList.add(vo);
		}
		return voList;
	}
	@Override
	public void saveMonitors(FreightMonitorSettingsVO vo) {
		// TODO Auto-generated method stub
		FreightMonitorSettingsEntity entityVO = new FreightMonitorSettingsEntity();
		entityVO.setDepartDate(vo.getDepartDate());
		entityVO.setFlightNumber(vo.getFlightNumber());
		entityVO.setFromCity(vo.getFromCity());
		entityVO.setToCity(vo.getToCity());
		entityVO.setOpDate(vo.getOpDate());
		entityVO.setId(vo.getId());
		entityVO.setAreaCode(vo.getAreaCode());
		entityVO.setFromCode(vo.getFromCode());
		entityVO.setToCode(vo.getToCode());
		entityVO.setHbaseKey(vo.getHbaseKey());
		entityVO.setCompanyCode(vo.getCompanyCode());
		entityVO.setCurrentUser(vo.getCurrentUser());
		entityVO.saveMonitors(entityVO);		
		
	}
	@Override
	public FreightMonitorSettingsVO getMonitorById(Long id) {

		FreightMonitorSettingsVO vo = new FreightMonitorSettingsVO();
		FreightMonitorSettingsEntity fo=new FreightMonitorSettingsEntity().getMonitorsById(id);
		if(fo !=null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			vo.setFromCity(fo.getFromCity());
			vo.setToCity(fo.getToCity());
			vo.setDepartDateString(sdf.format(fo.getDepartDate()));
			vo.setOpDateString(sdf.format(fo.getOpDate()));
			vo.setFlightNumber(fo.getFlightNumber());
			vo.setId(fo.getId());
			vo.setAreaCode(fo.getAreaCode());
			vo.setFromCode(fo.getFromCode());
			vo.setToCode(fo.getToCode());
			vo.setCompanyCode(fo.getCompanyCode());
			vo.setCurrentUser(fo.getCurrentUser());
			vo.setHbaseKey(fo.getHbaseKey());
		}
		return vo;
	}
	@Override
	public void saveOrUpdate(FreightMonitorSettingsVO fcVo) throws Exception {
		FreightMonitorSettingsEntity fen =new FreightMonitorSettingsEntity();
//		FreightMonitorSettingsEntity entityVO = new FreightMonitorSettingsEntity();
//		if(fen.getId() != null){
//			fen.getMonitorsById(fen.getId());
//		}
		fen.setId(fcVo.getId());
		fen.setFlightNumber(fcVo.getFlightNumber());
		fen.setFromCity(fcVo.getFromCity());
		fen.setToCity(fcVo.getToCity());
		fen.setOpDate(fcVo.getOpDate());
		fen.setDepartDate(fcVo.getDepartDate());
		fen.setAreaCode(fcVo.getAreaCode());
		fen.setFromCode(fcVo.getFromCode());
		fen.setToCode(fcVo.getToCode());
		fen.setCompanyCode(fcVo.getCompanyCode());
		fen.setCurrentUser(fcVo.getCurrentUser());
		fen.setHbaseKey(fcVo.getHbaseKey());
		
		fen.save(fen);
	}
	
	
	
	
	
	
}
